using System;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using FormsGenerator.Xml;

namespace FormsGenerator
{
	/// <summary>
	/// Summary description for ViewSource.
	/// </summary>
	public partial class ViewSource : System.Web.UI.Page
	{

		protected XmlDocument formXmlDocument;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if (Request["id"] == null || Request["id"].Equals(""))
			{
				//Use Server.Transfer to support Reverse proxy. Do not use Response.Redirect!
				Server.Transfer("default.aspx");
			}
			else
			{
				ViewState["GUID"] = Request["id"].Trim();
			}

			if (!IsPostBack)
			{
				try
				{
					formXmlDocument = XmlHandler.GetForm((string) ViewState["GUID"]);
				}
				catch (Exception ex)
				{
					LabelErrorMessage.Visible = true;
					LabelErrorMessage.Text += ex.Message;
				}
				StringBuilder xml = new StringBuilder();
				StringWriter stringWriter = new StringWriter(xml);
				XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
				xmlTextWriter.Formatting = Formatting.Indented;
				formXmlDocument.WriteContentTo(xmlTextWriter);
				TextBoxCode.Text = xml.ToString();
				Render();
			}
		}

		#region Web Form Designer generated code

		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}

		#endregion

		protected void ButtonUpdate_Click(object sender, System.EventArgs e)
		{
			formXmlDocument = XmlHandler.ParseXml(TextBoxCode.Text);
			try
			{
				XmlHandler.SaveForm((string) ViewState["GUID"], formXmlDocument, HttpContext.Current.User.Identity.Name);
				LabelExecuteJavascript.Text = "<script>updateForm('" + (string) ViewState["GUID"] + "', false);</script>";
				LabelExecuteJavascript.Visible = true;
				Render();
			}
			catch (Exception ex)
			{
				LabelErrorMessage.Visible = true;
				LabelErrorMessage.Text += ex.Message;
			}
		}

		protected void ButtonUpdateAndClose_Click(object sender, System.EventArgs e)
		{
			formXmlDocument = XmlHandler.ParseXml(TextBoxCode.Text);
			try
			{
				XmlHandler.SaveForm((string) ViewState["GUID"], formXmlDocument, HttpContext.Current.User.Identity.Name);
				LabelExecuteJavascript.Text = "<script>updateForm('" + (string) ViewState["GUID"] + "', true);</script>";
				LabelExecuteJavascript.Visible = true;
				Render();
			}
			catch (Exception ex)
			{
				LabelErrorMessage.Visible = true;
				LabelErrorMessage.Text += ex.Message;
			}
		}

		private void Render()
		{
			LabelFormName.Text = formXmlDocument.SelectSingleNode("/form/properties/name").InnerText;
		}
	}
}